/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.lottery.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.MapListUtils;
import com.thinkgem.jeesite.modules.lottery.entity.Activity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.lottery.entity.Prize;
import com.thinkgem.jeesite.modules.lottery.service.PrizeService;

import java.util.HashMap;
import java.util.Map;

/**
 * 活动奖品管理Controller
 * @author Muser
 * @version 2019-07-31
 */
@Controller
@RequestMapping(value = "${adminPath}/lottery/prize")
public class PrizeController extends BaseController {

	@Autowired
	private PrizeService prizeService;
	@Autowired
	private MapListUtils mapListUtils;
	
	@ModelAttribute
	public Prize get(@RequestParam(required=false) String id) {
		Prize entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = prizeService.get(id);
		}
		if (entity == null){
			entity = new Prize();
		}
		return entity;
	}
	
	@RequiresPermissions("lottery:prize:view")
	@RequestMapping(value = {"list", ""})
	public String list(Prize prize, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Prize> page = prizeService.findPage(new Page<Prize>(request, response), prize); 
		model.addAttribute("page", page);
		model.addAttribute("activityList",mapListUtils.getActivities());
		Map<String,String> activityMap = new HashMap<>();
		for (Activity activity:mapListUtils.getActivities()){
			activityMap.put(activity.getId(),activity.getName());
		}
		model.addAttribute("activityMap",activityMap);
		return "modules/lottery/prizeList";
	}

	@RequiresPermissions("lottery:prize:view")
	@RequestMapping(value = "form")
	public String form(Prize prize, Model model) {
		model.addAttribute("prize", prize);
		model.addAttribute("activityList",mapListUtils.getActivities());
		return "modules/lottery/prizeForm";
	}

	@RequiresPermissions("lottery:prize:edit")
	@RequestMapping(value = "save")
	public String save(Prize prize, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, prize)){
			return form(prize, model);
		}
		prizeService.save(prize);
		addMessage(redirectAttributes, "保存活动奖品成功");
		return "redirect:"+Global.getAdminPath()+"/lottery/prize/?repage";
	}
	
	@RequiresPermissions("lottery:prize:edit")
	@RequestMapping(value = "delete")
	public String delete(Prize prize, RedirectAttributes redirectAttributes) {
		prizeService.delete(prize);
		addMessage(redirectAttributes, "删除活动奖品成功");
		return "redirect:"+Global.getAdminPath()+"/lottery/prize/?repage";
	}

}